package threads;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * @author apple
 * @Classname CallableCreateTest
 * @Description 实现Callable接口创建线程
 * @Date 2020/4/19 16:07
 */
public class CallableCreateTest {
    public static void main(String[] args) throws Exception {
        MyCallable myCallableThread1 = new MyCallable();
        FutureTask futureTask1=new FutureTask<>(myCallableThread1);
        new Thread(futureTask1).start();

        MyCallable myCallableThread2 = new MyCallable();
        FutureTask futureTask2=new FutureTask<>(myCallableThread2);
        new Thread(futureTask2).start();
        Integer sum1 =(Integer) futureTask1.get();
        System.out.println("sum1="+sum1);
        Integer sum2=(Integer) futureTask2.get();
        System.out.println("sum2="+sum2);
    }
}

class MyCallable implements Callable<Integer> {

    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    @Override
    public Integer call() throws Exception {
        System.out.println("MyCallable name =" + Thread.currentThread()+"--start");
        int sum = 0;
        int count =10000;
        for(int i=0;i<count;i++){
            sum += i;
        }
        Thread.sleep(1000);
        System.out.println("MyCallable name =" + Thread.currentThread()+"--end");
        return sum;
    }
}
